import { Component, ViewChild, Host, ChangeDetectorRef } from '@angular/core';
import { AppComponent } from '../../../app.component';
import { UtilsService } from '../../../shared/services/utils.service';
import { ResearchCorporateGovernanceService } from './researchCorporateGovernance.service';
import { Subscription } from 'rxjs/Subscription';
import { ColumnOptions } from '../../../shared/components/table/models/columnOptions.model';
import { PaginationPositionDisplayEnum, TableComponent } from '../../../shared/components/table/table.component';
import { TableOptions } from '../../../shared/components/table/models/tableOptions.model';
import { CorporateGovernanceFilterModel } from './models/corporateGovernanceFilter.model';
import { AssessmentList } from '../../../shared/models/assessment.enum';
import { TableRouterLink } from '../../../shared/components/table/models/tableRouterLink.model';
import { AnalyticsService } from '../../../shared/services/googleAnalytics.service';
import { GoogleAnalyticsBeacon } from '../../../shared/models/googleAnalytics.model';
import { BaseComponent } from '../../../shared/components/baseComponent/base.component';


/**
 * Corporate Governance main component
 *
 * Author: Andrei Istrate
 * Date: 29.06.2018
 */

@Component({
    moduleId: module.id,
    templateUrl: 'researchCorporateGovernance.component.html',
    styleUrls: ['../research.component.scss'],
    providers: [UtilsService, ResearchCorporateGovernanceService]
})
export class ResearchCorporateGovernanceComponent extends BaseComponent {
    constructor(@Host() private rootComponent: AppComponent,
        private corporateGovernanceService: ResearchCorporateGovernanceService,
        private analyticsService: AnalyticsService,
        private cdRef: ChangeDetectorRef
    ) {
        super(cdRef);
    }


    public sub: Subscription;
    public assessmentsList: any[];
    public columns: ColumnOptions[];
    public subIndustryList: any[];
    public source: any[];
    public filter: CorporateGovernanceFilterModel = new CorporateGovernanceFilterModel;
    public elementCount: number;
    public paginationPosition = PaginationPositionDisplayEnum.UpAndDown;

    @ViewChild('table') table: TableComponent;

    ngOnInit() {
        this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('Corporate Governance Landing Page', 'Landing Pages', {}));
        if(!this.productPermissions.governance) this.rootComponent.finishLoading();
        this.source = [];
        this.setColumns();
        this.getBulkCompanies(new TableOptions("CompanyName"));
    }

    public getBulkCompanies(tableOptions: TableOptions) {
        this.corporateGovernanceService.getCompaniesCount(tableOptions.filter).subscribe(result => {
            this.elementCount = result.companyCount;
        });

        this.corporateGovernanceService.getCompanies(tableOptions.filter, tableOptions.pageOptions.skip, tableOptions.pageOptions.take, tableOptions.orderByCol, tableOptions.orderByAsc).subscribe(result => {
            let valuesToDisplay: any[];
            valuesToDisplay = result.map(company => {
                return {
                    CompanyNameLowerCase: new TableRouterLink(company.companyName, company.isEnabled,  UtilsService.getCompanyUrlFriendlyName(company) , 'governance'),
                    exchange: company.exchange,
                    ticker: company.ticker,
                    performance: AssessmentList[company.performance] ,
                    subPeerGroup: company.subPeerGroup,
                };
            });
            this.source = valuesToDisplay;
        });
    }

    private setColumns() {
        this.columns = [
            new ColumnOptions('CompanyName', 'companyName', 'Company Names', 'col-md-3', true, undefined, false, null, false, true, false),
            new ColumnOptions('Exchange', 'Exchange', 'Exchange', 'col-md-1', false, undefined, false, null, false, false, false,true, undefined, false, false, true),
            new ColumnOptions('Ticker', 'Ticker', 'Ticker', 'col-md-1', false, undefined, false, null, false, false, false,true, undefined, false, false, true),
            new ColumnOptions('Performance', 'Performance', 'Overall CG performance', 'col-md-3', false, undefined, true, null, false, false, false, true, undefined, false, false, true),
            new ColumnOptions('Subindustry', 'Subindustry', 'Subindustry', 'col-md-3', false, undefined, true,  /* this.subIndustryList */ null, false, false, false, true,undefined, false, false, true),
        ];
    }

    public tableOptionsChanged(tableOptions: TableOptions): void {
        this.getBulkCompanies(tableOptions);
    }

    ngAfterViewInit() {
        this.rootComponent.finishLoading();
    }

}